package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.dao.jdbc.ParentJdbc;
import com.wuseguang.springcloud.dao.repository.ParentRepository;
import com.wuseguang.springcloud.entity.Parent;
import com.wuseguang.springcloud.service.ParentService;
import com.wuseguang.springcloud.util.MyDES;
import com.wuseguang.springcloud.util.Result;
import com.wuseguang.springcloud.util.UpdateTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 家长信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-12-29
 */
@Service
public class ParentServiceImpl implements ParentService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ParentRepository parentRepository;
    @Autowired
    private ParentJdbc parentJdbc;

    @Override
    public Result findParent(String parentId) {
        logger.info("查询家长信息");
        String sql = "select id,phone,password,name,sex from parent where id = ?";
        Map<String,Object> map = null;
        try {
            map =  parentJdbc.queryMap(sql,parentId);
        }catch (Exception e){
            map = new HashMap<>();
        }
        return new Result(map);
    }

    @Override
    public Result updateParent(Parent parent) {
        logger.info("修改家长信息");
        //加密密码
        parent.setPassword(MyDES.encryptBasedDes(parent.getPassword()));
        Parent parentTwo =  parentRepository.findById(parent.getId()).get();
        UpdateTool.copyNullProperties(parentTwo, parent);
        parentRepository.save(parent);
        return new Result();
    }
}
